package com.eddie.auction.service.impl;

import com.eddie.auction.dao.BaseDao;
import com.eddie.auction.dao.IBaseDao;
import com.eddie.auction.service.IService;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * @author Eddie
 * @date 2022/08/01 15:20
 **/
public class ServiceImpl<D extends IBaseDao<T>, T> implements IService<T> {
    protected D baseDao;

    {
        try {
            //获取第零个泛型类
            if(this.getClass().getGenericSuperclass() instanceof ParameterizedType) {

            }
            Class<D> dao = (Class<D>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
            //通过泛型类型创建实例
            String dapImpl = dao.getPackageName() + ".impl";
            //获取dao包下impl包对应的实现类
            baseDao = (D) Class.forName(dapImpl + "."
                    + dao.getName().substring(dao.getName().lastIndexOf(".")
                    + 1)+"Impl").getDeclaredConstructor().newInstance();
//            baseDao =aPackage;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    ;

    @Override
    public List<T> list() {
        return baseDao.list();
    }
}
